package com.mulanweb.api.module.controller;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.*;

/**
 * Created by Administrator on 2016/8/19.
 */
@ValidateFile
public class SellerController extends BaseController
{
    public void searchContent(){
        int typeId = getParaToInt("typeId");
        long targetId = getParaToLong("targetId");
        Long cityId = null;
        Long sellerId = 0L;
        SQLGenerateHelper helper = null;
        boolean tag = true;
        if(typeId ==23){
            tag = false;
            Ticket ticket = Ticket.dao.findById(targetId);
            if(StrKit.notNull(ticket)){
                sellerId = ticket.getSellerId();
                cityId = ticket.getCityId();
                helper = new SQLGenerateHelper("ticket","create_time");
                helper.addCondition("surplus_total",">",0);
            }
        }else if(typeId == 24){
            Chartered chartered = Chartered.dao.findById(targetId);
            if(StrKit.notNull(chartered)){
                sellerId = chartered.getSellerId();
                cityId = chartered.getCityId();
                helper = new SQLGenerateHelper("chartered","create_time");
            }
        }
        helper.addEqualsCondition("seller_id",sellerId);
        helper.addIsNullCondition("delete_time");
        helper.addIsNullCondition("not_available_time");
        helper.addEqualsCondition("city_id",cityId);
        Page<?> page ;
        if(tag){
             page = Chartered.dao.paginate(1,100,helper.getSqlSelect(),helper.getSqlExceptSelect(),helper.getQueryParas());
        }else{
            page = Ticket.dao.paginate(1,100,helper.getSqlSelect(),helper.getSqlExceptSelect(),helper.getQueryParas());
        }
        renderJson(page);
    }

    public void requestInfo()
    {
        Long    targetId = super.getParaToLong("targetId");
        Integer typeId   = super.getParaToInt("typeId");
        String  typeName = super.getPara("typeName");
        String  openId   = super.getSessionAttr("openId");//"oULNBwOvt_D2A_8se1bQOQ06ky0w";//
        if (StrKit.notBlank(openId))
        {
            User user = User.dao.findFirst("SELECT * FROM `user` WHERE open_id = ?", openId);
            if (StrKit.notNull(user))
            {
                List<Integer> ticketType = new ArrayList<Integer>(3);
                ticketType.add(1);
                ticketType.add(2);
                ticketType.add(3);
                ticketType.add(23);
                if(ticketType.contains(typeId)){
                    typeId = 23;//门票券票
                }else{
                    typeId = 24;//包车陪游
                }
                SQLGenerateHelper helper = new SQLGenerateHelper("`order`");
                helper.addEqualsCondition("type_id", typeId);
                helper.addEqualsCondition("target_id", targetId);
                helper.addEqualsCondition("user_id", user.getId());
                Order order = Order.dao.findFirst(helper.getSqlSelect() + helper.getSqlExceptSelect(), helper.getQueryParas());
                Map<String, Object> map    = new HashMap<String, Object>();
                if (StrKit.notNull(order))
                {
                    Seller seller = Seller.dao.findById(order.getSellerId());
                    map.put("ReturnCode", 1);
                    if ("email".equals(typeName))
                    {
                        map.put("ReturnStr", "联系Email:" + seller.getEmail());
                    } else if ("mobile".equals(typeName))
                    {
                        map.put("ReturnStr", "联系Mobile:" + seller.getMobileNumber());
                    }
                }else{
                    map.put("ReturnCode",0);
                    map.put("ReturnStr","您必须购买此商品之后才能才看此信息;");
                }
                renderJson(map);
            }else{
                returnJson(0,Constant.PromptMessage.NOT_EXIST_USER);
            }
        }else{
            returnJson(0, Constant.PromptMessage.NOT_EXIST_USER);
        }
    }

    public void requestVisaInfo(){
        Long    targetId = super.getParaToLong("targetId");
        String  typeName = super.getPara("typeName");
        String  openId   = super.getSessionAttr("openId");//"oULNBwOvt_D2A_8se1bQOQ06ky0w";//
        if (StrKit.notBlank(openId))
        {
            User user = User.dao.findFirst("SELECT * FROM `user` WHERE open_id = ?", openId);
            if (StrKit.notNull(user))
            {
                SQLGenerateHelper helper = new SQLGenerateHelper("`visa_order`");
                List<VisaChild> visaChildList = VisaChild.dao.find("SELECT * FROM visa_child WHERE parent_id =? ",targetId);
                List<Integer> visaChilds = new ArrayList<>();
                for(VisaChild vc : visaChildList){
                    visaChilds.add(vc.getId());
                }
                helper.addEqualsCondition("visa_child_id", visaChilds);
                helper.addEqualsCondition("user_id", user.getId());
                List<Long> statusIds = new ArrayList<>();//合法状态
                statusIds.add(4L);
                statusIds.add(7L);
                statusIds.add(8L);
                statusIds.add(23L);
                statusIds.add(25L);
                helper.addInConditionToLong("status_id",statusIds);
                VisaOrder visaOrder = VisaOrder.dao.findFirst(helper.getSqlSelect() + helper.getSqlExceptSelect(), helper.getQueryParas());
                Map<String, Object> map    = new HashMap<String, Object>();
                if (StrKit.notNull(visaOrder))
                {
                    map.put("ReturnCode", 1);
                    if ("email".equals(typeName))
                    {
                        map.put("ReturnStr", "联系Email:" +"visa@nzassistant.co.nz");
                    } else if ("mobile".equals(typeName))
                    {
                        map.put("ReturnStr", "联系Mobile:" + "0574-27989229");
                    }
                }else{
                    map.put("ReturnCode",0);
                    map.put("ReturnStr","您必须购买此商品之后才能才看此信息;");
                }
                renderJson(map);
            }else{
                returnJson(0,Constant.PromptMessage.NOT_EXIST_USER);
            }
        }else{
            returnJson(0, Constant.PromptMessage.NOT_EXIST_USER);
        }
    }

}
